import winston, { format } from 'winston';
import { inspect } from 'util';
const myFormat = format.printf(info => {
	const time = new Date().toLocaleString();
	return `${time} [${info.label}] ${info.level}: ${info.message}`;
});

export const logger = winston.createLogger({
	level: 'info',
	format: myFormat,
	transports: [
		new winston.transports.File({ filename: 'error.log', level: 'error' }),
		new winston.transports.File({ filename: 'combined.log' }),
	],
});
if (process.env.NODE_ENV != 'test') {
}
if (process.env.NODE_ENV == 'development') {
	logger.add(
		new winston.transports.Console({
			format: myFormat,
		}),
	);
}

export const makeLabelLogger = function(label) {
	const convertArgs = args => {
		let cargs = args.map(v => {
			if (typeof v == 'string') return v;
			if (typeof v == 'number') return v;
			return inspect(v);
		});
		return cargs;
	};
	const llog = (...args) => {
		let cargs = convertArgs(args);
		logger.info(cargs.join(', '), { label });
	};
	const lerror = (...args) => {
		let cargs = convertArgs(args);
		logger.error(cargs.join(', '), { label });
	};
	return { llog, lerror };
};

export const { llog, lerror } = makeLabelLogger('wxd');
llog('************************');
llog(`*** start pid: ${process.pid} ***`);
llog('************************');
